package com.thanhloyal.exchanger.utils;

import java.util.ArrayList;

import com.thanhloyal.exchanger.Currency;
import com.thanhloyal.exchanger.RateData;
import com.thanhloyal.exchanger.RateDataItem;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SchemaHelper extends SQLiteOpenHelper {

	private static final String dbName = "exchanger.db";
	
	public SchemaHelper(Context ctx){
		super(ctx, dbName, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}
	
	
	
	public ArrayList<RateDataItem> getAllRatesData(){
		ArrayList<RateDataItem> rates = new ArrayList<RateDataItem>();
		
		SQLiteDatabase db = getWritableDatabase();
		try {
			Cursor cursor = db.query(RateDataItemTable.tbName,
					new String[] { 
						RateDataItemTable.pairCol, 
						RateDataItemTable.askRateCol, 
						RateDataItemTable.bidRateCol 
					}, 
					null, null, null, null, null);
			
			//+-------+-----------+---------+
			//|Pair   | AskRate   | BidRate |
			while(cursor.moveToNext()){
				String pair = cursor.getString(0);
				double askRate = cursor.getDouble(1);
				double bidRate = cursor.getDouble(2);
				
				RateDataItem row = new RateDataItem(pair, askRate, bidRate);
				rates.add(row);
			}
		} catch (Exception e) {
			Log.e("Loyal", "getAllRatesData(): "+e.getMessage());
			
		}
		
		return rates;
	}

	
	class RateDataItemTable{
		public static final String tbName = "RateData";
		public static final String pairCol = "Pair";
		public static final String askRateCol = "AskRate";
		public static final String bidRateCol = "BidRate";
		
	}
}
